import java.util.Random;

/**
 * This class is responsible for generating random events based and decided when
 * and to whom to apply them.
 * 
 * @author GT Five-O
 * @version v1.0b
 * 
 */
public class RandomEvent implements java.io.Serializable {
    private Random rand = new Random();
    private boolean turnIsActive = false;
    private GameScreen board;

    /**
     * constructor that sets assigns the instance variable with the game board
     * value passed in
     * 
     * @param board
     */
    public RandomEvent(GameScreen board) {
        this.board = board;
    }

    /**
     * method that sets the boolean value, turn. The method gets a random number
     * from 0-99 and if it is less than 26, turn is set to true. If not, it gets
     * set to false.
     */
    public void RandTurn() {
        int num = rand.nextInt(100);
        if (num <= 26) {
            turnIsActive = true;
        } else {
            turnIsActive = false;
        }
    }

    /**
     * This method takes in a boolean and the turn number. If the boolean is
     * true, it gets a random number from 0-3 and adds 1 to it. Else, it gets a
     * random number from 0-7. Then, if the instance variable turn is true, it
     * gives the player additional resources based on the random number
     * generated earlier. If it is 1, the player gets 3 food and 2 energy units.
     * If it is 2, the player gets 2 ores. If 3 is the random number, the player
     * gets money equaling their turn number + 1 times 8. If it is 4, the player
     * gets money equaling their turn number + 1 times 2. If it is 5, the player
     * gets money equaling their turn number + 1 times 4. If the number is 6,
     * the player loses half of their food. Finally, if the number is 7, the
     * player loses their turn number + 1 times 6 of their money. Each of these
     * conditionals, if executed, returns a string of the resources gained/lost
     * by the player.
     * 
     * @param turnNum
     * @param b
     * @return
     */
    public String RandEvent(int turnNum, boolean b) {
        int turnNum2 = turnNum + 1;
        int num = 0;
        if (b)
            num = rand.nextInt(4) + 1;
        else {
            num = rand.nextInt(8);
        }
        if (turnIsActive) {
            turnIsActive = false;
            if (num == 1) {
                board.getCurrent().addFood(new Food());
                board.getCurrent().addFood(new Food());
                board.getCurrent().addFood(new Food());
                board.getCurrent().addEnergy(new Energy());
                board.getCurrent().addEnergy(new Energy());
                return "YOU JUST RECEIVED A PACKAGE FROM THE GT ALUMNI CONTAINING 3 FOOD AND 2 ENERGY UNITS.";
            } else if (num == 2) {
                board.getCurrent().addOre(new Ore());
                board.getCurrent().addOre(new Ore());
                return "A WANDERING TECH STUDENT REPAID YOUR HOSPITALITY BY LEAVING TWO BARS OF ORE.";
            } else if (num == 3) {
                board.getCurrent().addMoney(8 * turnNum2);
                return "THE MUSEUM BOUGHT YOUR ANTIQUE PERSONAL COMPUTER FOR $ "
                        + 8 * turnNum2 + ".";
            } else if (num == 4) {
                board.getCurrent().addMoney(2 * turnNum2);
                return "YOU FOUND A DEAD MOOSE RAT AND SOLD THE HIDE FOR $ "
                        + 2 * turnNum2 + ".";
            } else if (num == 5) {
                board.getCurrent().buying(4 * turnNum2);
                return "FLYING CAT-BUGS ATE THE ROOF OFF YOUR HOUSE. REPAIRS COST $ "
                        + 4 * turnNum2 + ".";
            } else if (num == 6) {
                board.getCurrent().removeHalfFood();
                return "MISCHIEVOUS UGA STUDENTS BROKE INTO YOUR STORAGE SHED AND STOLE HALF YOUR FOOD.";
            } else if (num == 7) {
                board.getCurrent().buying(6 * turnNum2);
                return "YOUR SPACE GYPSY INLAWS MADE A MESS OF THE TOWN. IT COST YOU $ "
                        + 6 * turnNum2 + "TO CLEAN IT UP.";
            }
        }
        return "";
    }

    /**
     * returns a string value of the RandEvent method above.
     * 
     * @param turnNum
     * @param b
     * @return
     */
    public String checkRandom(int turnNum, boolean b) {
        RandTurn();
        String message = RandEvent(turnNum, b);
        return message;
    }
}
